package com.avic.rong.activity.no;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.avic.rong.MyApplication;
import com.avic.rong.R;
import com.avic.rong.bean.UserArgBean;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UserFavoriteListActivity extends AppCompatActivity {


    public static String ARG_URL = "arg_url";

    @BindView(R.id.webview)
    BridgeWebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_money_list);
        ButterKnife.bind(this);

        String url = null;
        if (getIntent() != null){
            url = getIntent().getStringExtra(ARG_URL);
        }

        if (!TextUtils.isEmpty(url)) {
            webView.loadUrl(url);
        }

        webView.registerHandler("pushVc", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {

                data = data.replace("\\","");
                JsonObject jsonObject = (JsonObject)new JsonParser().parse(data);
                String currentUrl = jsonObject.get("url").getAsString();
                if (currentUrl.contains("good/info")) {
                    startActivity(CriterionDetailActivity.queryIntent(UserFavoriteListActivity.this, currentUrl));
                }else{

                }
            }
        });

        webView.registerHandler("goBack", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                finish();
            }
        });

        webView.registerHandler("queryUserId", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                UserArgBean userArgBean = new UserArgBean();
                if (MyApplication.getInstance().userLoginBean != null) {
                    userArgBean.setUserIdSign(String.valueOf(MyApplication.getInstance().userLoginBean.getUserId()));
                } else {
                    userArgBean.setUserIdSign("");
                }
                if (webView != null) {
                    Gson gson = new Gson();
                    function.onCallBack(gson.toJson(userArgBean));
                }
            }
        });
    }

    public void setLoginUserId() {
        UserArgBean userArgBean = new UserArgBean();
        if (MyApplication.getInstance().userLoginBean != null) {
            userArgBean.setUserIdSign(String.valueOf(MyApplication.getInstance().userLoginBean.getUserId()));
        } else {
            userArgBean.setUserIdSign("");
        }
        if (webView != null) {
            Gson gson = new Gson();
            webView.callHandler("getUserIdTwo", gson.toJson(userArgBean), new CallBackFunction() {
                @Override
                public void onCallBack(String data) {
                    System.out.println("----------------------wang111" + data);
                }
            });
        }
    }


    public static Intent queryIntent(Context context,String url){
        Intent intent = new Intent(context,UserFavoriteListActivity.class);
        intent.putExtra(ARG_URL,url);
        return intent;
    }


}
